package com.hsd.commons.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hsd.commons.model.HsdAppCodeIndex;
import com.hsd.commons.model.HsdRole;
import com.hsd.commons.service.IAuthService;
import com.hsd.commons.util.DateUtil;
import com.hsd.core.cache.DictCache;
import com.hsd.core.page.entity.PageParams;
import com.hsd.core.page.entity.Pagination;
import com.hsd.survey.SurveyConstants;

/**
 * 角色管理
 * @author Thinkpad
 *
 */

@Controller
@RequestMapping("/auth/role")
public class RoleManageAction {
	@Autowired
	private IAuthService authService;
	@RequestMapping(value="/loadRoleList.do")
	public ModelAndView loadRoleList(PageParams pageParams,HttpServletRequest request,ModelMap model){
		String roleName = request.getParameter("roleName");
		Pagination page = authService.loadRoleList(pageParams,roleName);
		
		model.addAttribute("page",page); 
		model.addAttribute("roleName",roleName);  
		return new ModelAndView("/app/module/role/roleList",model); 
	}
	/**
	 * 新增一个角色
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/addOneRole.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String addOneRole(HsdRole role,HttpServletRequest request,ModelMap model){
		Map query = new HashMap();
		query.put("rolecode", role.getRolecode());
		Object existData = authService.getUniqueObject(HsdAppCodeIndex.class, query);
		if(null != existData){
			return "该角色已存在！";
		} 
		role.setCreatedate(new Long(DateUtil.currentDate()));
		role.setIsuse(1l);
		role.setId(this.authService.getSeqNextVal("seq_hsd_role"));
		//role.setCreateuser(createuser);
		authService.save(role); 
		 
		return "保存成功！";
	}
	
	/**
	 * 修改一个角色
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/editOneRole.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String editOneRole(HsdRole role,HttpServletRequest request,ModelMap model){ 
		authService.save(role);
		return "修改成功！";
	}
	
	/**
	 * 删除角色
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/delOneRole.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String delOneRole(HttpServletRequest request,ModelMap model){
		String id = request.getParameter("id"); 
		HsdRole roleObj = (HsdRole)authService.findByID(HsdRole.class, new Long(id));
		roleObj.setIsuse(0l);
		authService.saveObject(roleObj);
		return "删除成功！";
	}
}
